package com.example.home.attorney.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.attorney.adapter.InitLawyerListAdapter;
import com.example.home.attorney.adapter.LegalExpertiseAdapter;
import com.example.home.attorney.adapter.UserClickAdapter;
import com.example.home.attorney.bean.InitLawyerListBean;
import com.example.home.attorney.bean.LawyerCardListBean;
import com.example.home.attorney.bean.UserClickBean;
import com.example.home.tool.OKHttpTool;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

// 定义排序方式的枚举类型
enum SortType {
    DEFAULT(R.string.sort_default),
    EXPERIENCE(R.string.sort_experience),
    SERVICE_COUNT(R.string.sort_service_count),
    GOOD_REVIEWS(R.string.sort_good_reviews);

    private final int displayNameResId;

    SortType(int displayNameResId) {
        this.displayNameResId = displayNameResId;
    }

    public String getDisplayName(AppCompatActivity context) {
        return context.getString(displayNameResId);
    }

}


public class MoreAttorneyList extends AppCompatActivity {
    private SortType currentSortType = SortType.DEFAULT;

    private ImageView back;
    private TextView qq;
    private Spinner sortSpinner;
    private TextView btnShaixuan;
    private RecyclerView rcvList;
    private List<InitLawyerListBean.RowsBean> initLawyerListBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_attorney_list);
        initView();
        // 初始化Spinner
        initSpinner();

        // 初始化律师列表
        initLawyerList();
    }

    private void initSpinner() {
        Spinner sortSpinner = findViewById(R.id.sortSpinner);

        // 创建排序方式的适配器
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // 添加枚举值的中文显示名称到适配器
        for (SortType sortType : SortType.values()) {
            adapter.add(sortType.getDisplayName(this));
        }

        // 设置适配器
        sortSpinner.setAdapter(adapter);

        // 设置选择监听器
        sortSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // 用户选择的排序方式
                currentSortType = SortType.values()[position];

                // 根据新的排序方式刷新律师列表
                loadLawyerList(currentSortType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // 未选择时的处理
            }
        });
    }

    private void initLawyerList() {
        // 初始化律师列表
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list",
                OKHttpTool.GET,
                InitLawyerListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        InitLawyerListBean response = (InitLawyerListBean) obj;
                        if (response != null) {
                            initLawyerListBean = response.getRows();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    bindList(initLawyerListBean);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        // 处理请求失败的情况
                    }
                });
    }

    private void bindList(List<InitLawyerListBean.RowsBean> lawyerList) {
        RecyclerView recyclerView = findViewById(R.id.rcv_list);
        InitLawyerListAdapter initLawyerListAdapter = new InitLawyerListAdapter(MoreAttorneyList.this, lawyerList);
        recyclerView.setAdapter(initLawyerListAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MoreAttorneyList.this);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    private void loadLawyerList(SortType sortType) {
        // 根据排序方式加载律师列表数据
        switch (sortType) {
            case DEFAULT:
                loadDefaultList();
                break;
            case EXPERIENCE:
                loadExperienceList();
                break;
            case SERVICE_COUNT:
                loadServiceCountList();
                break;
            case GOOD_REVIEWS:
                loadGoodReviewsList();
                break;
        }
    }


    private void loadDefaultList() {
        // 处理默认排序方式的逻辑
        initLawyerList();
    }

    private void loadServiceCountList() {
        //根据好评人数
        List<InitLawyerListBean.RowsBean> lawyerList = getSortedLawyerListByServiceTimesDescending();
        bindList(lawyerList);
    }

    private List<InitLawyerListBean.RowsBean> getSortedLawyerListByServiceTimesDescending() {
        List<InitLawyerListBean.RowsBean> lawyerList = initLawyerListBean;
        sortByServiceTimesDescending(lawyerList);
        return lawyerList;
    }
    private void sortByServiceTimesDescending(List<InitLawyerListBean.RowsBean> lawyerList) {
        Collections.sort(lawyerList, new Comparator<InitLawyerListBean.RowsBean>() {
            @Override
            public int compare(InitLawyerListBean.RowsBean lawyer1, InitLawyerListBean.RowsBean lawyer2) {
                // 反转比较结果，以实现从高到低排序
                return Integer.compare(lawyer2.getFavorableCount(), lawyer1.getFavorableCount());
            }
        });
    }




    private void loadGoodReviewsList() {
        // 处理好评率排序方式的逻辑
        List<InitLawyerListBean.RowsBean> lawyerList = getSortedLawyerListByFavorableRate();
        bindList(lawyerList);
    }

    private void sortByFavorableRate(List<InitLawyerListBean.RowsBean> lawyerList) {
        Collections.sort(lawyerList, new Comparator<InitLawyerListBean.RowsBean>() {
            @Override
            public int compare(InitLawyerListBean.RowsBean lawyer1, InitLawyerListBean.RowsBean lawyer2) {
                // 反转比较结果，以实现从高到低排序
                return Double.compare(lawyer2.getFavorableRate(), lawyer1.getFavorableRate());
            }
        });
    }

    private List<InitLawyerListBean.RowsBean> getSortedLawyerListByFavorableRate() {
        List<InitLawyerListBean.RowsBean> lawyerList = initLawyerListBean;
        sortByFavorableRate(lawyerList);
        return lawyerList;
    }





    private void loadExperienceList() {
        // 处理从业年限排序方式的逻辑
        List<InitLawyerListBean.RowsBean> lawyerList = getSortedLawyerListByExperience(); // 获取已经排序的律师列表
        bindList(lawyerList); // 刷新UI
    }

    private List<InitLawyerListBean.RowsBean> getSortedLawyerListByExperience() {
        // 获取原始律师列表
        List<InitLawyerListBean.RowsBean> lawyerList = initLawyerListBean;

        // 使用日期进行排序
        sortByExperience(lawyerList);

        return lawyerList;
    }

    private void sortByExperience(List<InitLawyerListBean.RowsBean> lawyerList) {
        Collections.sort(lawyerList, new Comparator<InitLawyerListBean.RowsBean>() {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            @Override
            public int compare(InitLawyerListBean.RowsBean lawyer1, InitLawyerListBean.RowsBean lawyer2) {
                try {
                    Date date1 = dateFormat.parse(lawyer1.getWorkStartAt());
                    Date date2 = dateFormat.parse(lawyer2.getWorkStartAt());
                    return date1.compareTo(date2);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }



    private void showFilterDialog() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-expertise/list",
                OKHttpTool.GET,
                LawyerCardListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LawyerCardListBean lawyerCardListBean = (LawyerCardListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                BottomSheetDialog filterDialog = new BottomSheetDialog(MoreAttorneyList.this);
                                filterDialog.setContentView(R.layout.dialog_filter);
                                RecyclerView expertiseRecyclerView = filterDialog.findViewById(R.id.rcv_list11);
                                if (expertiseRecyclerView != null) {
                                    expertiseRecyclerView.setLayoutManager(new GridLayoutManager(MoreAttorneyList.this, 4));
                                    LegalExpertiseAdapter expertiseAdapter = new LegalExpertiseAdapter(lawyerCardListBean.getRows(), filterDialog, new LegalExpertiseAdapter.Callback() {
                                        @Override
                                        public void onItemClick(int position) {
                                            getUserClick(position);
                                        }
                                    });
                                    expertiseRecyclerView.setAdapter(expertiseAdapter);
                                }

                                // Show the dialog
                                filterDialog.show();
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });

    }

    private void getUserClick(int position) {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list?legalExpertiseId="+position,
                OKHttpTool.GET,
                UserClickBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        UserClickBean userClickBean = (UserClickBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (userClickBean.getTotal() == 0){
                                    Toast.makeText(MoreAttorneyList.this,"占无该分类律师数据",Toast.LENGTH_SHORT).show();
                                }else {
                                    UserClickAdapter lawyerCardListAdapter = new UserClickAdapter(MoreAttorneyList.this, userClickBean.getRows());
                                    rcvList.setAdapter(lawyerCardListAdapter);
                                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MoreAttorneyList.this);
                                    rcvList.setLayoutManager(linearLayoutManager);
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }


    private void initView() {
        back = findViewById(R.id.back);
        sortSpinner = findViewById(R.id.sortSpinner);
        btnShaixuan = findViewById(R.id.btn_shaixuan);
        rcvList = findViewById(R.id.rcv_list);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MoreAttorneyList.this.finish();
            }
        });

        btnShaixuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFilterDialog();
            }
        });
    }
}


